﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PO_2011.Popup
{
    public partial class Morfologia : Form
    {
        Gimp gimp;
        CheckBox[] checkBoxex;
        int[,] checkboxes;

        public Morfologia(Gimp program)
        {
            this.gimp = program;
            InitializeComponent();
        }

        private void buttonZastosuj_Click(object sender, EventArgs e)
        {
            this.gimp.gimpTimer();

            int size = 3;

            switch (comboBoxSize.Text)
            {
                case "3x3":
                    break;
                case "5x5":
                    size = 5;
                    break;
                default:
                    break;
            }

            if (this.checkBoxex != null)
            {
                checkboxes = new int[this.checkBoxex.Length, this.checkBoxex.Length];

                int x = -1, y = -1;
                int max = this.checkBoxex.Length / size;
                for (int i = 0; i < max; i++)
                {
                    if (i % size == 0)
                    {
                        x = 0;
                        y++;
                    }
                    else x++;

                    checkboxes[x, y] = this.checkBoxex[i].Checked == true ? 1 : 0;
                }
            }

            ImageHistory.History.addStep(new Bitmap(this.gimp.imageBox.Image));

            this.gimp.imageBox.Image = ColorConvert.Colors.convertFromColorToBlack(new Bitmap(this.gimp.imageBox.Image), "P4", this.gimp.img.Header.MagicNumber);
                    
            switch (comboBoxMorfologia.Text)
            {
                case "erozja":
                    this.gimp.imageBox.Image = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "erozja", size, checkboxes);
                    this.gimp.imageBox.Refresh();
                    break;
                case "dylacja":
                    this.gimp.imageBox.Image = this.gimp.img.bitmap = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "dylacja", size, checkboxes);
                    this.gimp.imageBox.Refresh();
                    break;
                case "otwarcia":
                    this.gimp.imageBox.Image = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "erozja", size, checkboxes);
                    this.gimp.imageBox.Image = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "dylacja", size, checkboxes);
                    this.gimp.imageBox.Refresh();
                    break;
                case "domknięcia":
                    this.gimp.imageBox.Image = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "dylacja", size, checkboxes);
                    this.gimp.imageBox.Image = Noise.Noise.create(new Bitmap(this.gimp.imageBox.Image), "erozja", size, checkboxes);
                    this.gimp.imageBox.Refresh();
                    break;
                default:
                    break;
            }

            this.gimp.gimpTimer();
            MessageBox.Show(comboBoxMorfologia.Text);
            this.Close();
        }

        private void comboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int breaker = -25;
            int ii = 0;

            if (this.checkBoxex != null)
            {
                for (int i = 0; i < this.checkBoxex.Length; i++)
                {
                    Controls.Remove(this.checkBoxex[i]);
                }
            }

            switch (comboBoxSize.Text)
            {
                case "3x3":
                    this.checkBoxex = new CheckBox[9];
                    for (int i = 0; i < 9; i++)
                    {
                        if (i % 3 == 0)
                        {
                            breaker += 20;
                            ii = 0;
                        }
                        else
                        {
                            ii++;
                        }
                        this.checkBoxex[i] = new CheckBox();
                        this.checkBoxex[i].Name = "checkbox" + i.ToString();
                        this.checkBoxex[i].Left = 200 + ii * 20;
                        this.checkBoxex[i].Top = 20 + breaker;
                        this.checkBoxex[i].Width = 15;
                        this.checkBoxex[i].Height = 14;
                        this.checkBoxex[i].Checked = true;
                        Controls.Add(this.checkBoxex[i]); 
                    }
                    break;
                case "5x5":
                    this.checkBoxex = new CheckBox[25];
                    for (int i = 0; i < 25; i++)
                    {
                        if (i % 5 == 0)
                        {
                            breaker += 20;
                            ii = 0;
                        }
                        else
                        {
                            ii++;
                        }


                        this.checkBoxex[i] = new CheckBox();
                        this.checkBoxex[i].Name = "checkbox" + i.ToString();
                        this.checkBoxex[i].Left = 200 + ii * 20;
                        this.checkBoxex[i].Top = 20 + breaker;
                        this.checkBoxex[i].Width = 15;
                        this.checkBoxex[i].Height = 14;
                        this.checkBoxex[i].Checked = true;
                        Controls.Add(this.checkBoxex[i]);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
